package com.groupbuying.currency.producer.service.impl;

import com.groupbuying.currency.producer.constants.CurrencyResultEnumConstants;
import com.groupbuying.currency.producer.domain.CurrencyCustomerLogDO;
import com.groupbuying.currency.producer.domain.CurrencyPrepayInfoDO;
import com.groupbuying.currency.producer.utils.currencyOperUtils.DateFormatHelper;
import com.groupbuying.currency.producer.utils.currencyOperUtils.UuidUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.groupbuying.currency.producer.dao.CurrencyResultDao;
import com.groupbuying.currency.producer.domain.CurrencyResultDO;
import com.groupbuying.currency.producer.service.CurrencyResultService;


@Service
public class CurrencyResultServiceImpl implements CurrencyResultService {
    private static Logger log = LoggerFactory.getLogger(CurrencyResultServiceImpl.class);
    @Autowired
    private CurrencyResultDao currencyResultDao;

    @Override
    public CurrencyResultDO get(String resultId) {
        return currencyResultDao.get(resultId);
    }

    @Override
    public List<CurrencyResultDO> list(Map<String, Object> map) {
        return currencyResultDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return currencyResultDao.count(map);
    }

    @Override
    public int save(CurrencyResultDO currencyResult) {
        return currencyResultDao.save(currencyResult);
    }

    @Override
    public int update(CurrencyResultDO currencyResult) {
        return currencyResultDao.update(currencyResult);
    }

    @Override
    public int remove(String resultId) {
        return currencyResultDao.remove(resultId);
    }

    @Override
    public int batchRemove(String[] resultIds) {
        return currencyResultDao.batchRemove(resultIds);
    }

    /**
     * 保存积分消费结果表
     *
     * @param
     * @return
     */
    @Override
    public int saveCurrencyResult(CurrencyCustomerLogDO currencyCustomerLog, CurrencyPrepayInfoDO prePayInfo) {
        CurrencyResultDO currencyResult = new CurrencyResultDO();
        currencyResult.setResultId(UuidUtils.createUuid());
        currencyResult.setBusinessType(CurrencyResultEnumConstants.ENUM_BUSINESSTYPE_CURRENCYPAY);
        currencyResult.setCreateTime(DateFormatHelper.getNowTimeStr());
        currencyResult.setCustomerId(currencyCustomerLog.getCustomerId());
        currencyResult.setDelFlag(CurrencyResultEnumConstants.ENUM_DELFLAG_UNDEL);
        currencyResult.setPlatformBusnum(prePayInfo.getPlatformBusnum());
        currencyResult.setOperType(CurrencyResultEnumConstants.ENUM_OPERTYPE_SUB);
        currencyResult.setOperCurrency(prePayInfo.getPayCurrency());
        currencyResult.setOpeTime(DateFormatHelper.getNowTimeStr());
        currencyResult.setPrepayNo(prePayInfo.getPrepayNo());
        currencyResult.setState(CurrencyResultEnumConstants.ENUM_STATE_SUCCESS);
        return this.save(currencyResult);
    }

    @Override
    public CurrencyResultDO queryPayStateByPrePayNo(String prePayNo) {
        Map map = new HashMap();
        map.put("prepayNo", prePayNo);
        map.put("delFlag", "1");
        List<CurrencyResultDO> list = this.list(map);
        return list != null && list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public void updateCurrencyResult(String prePayNo) {

        Map map = new HashMap();
        map.put("prepayNo", prePayNo);
        map.put("delFlag", "1");
        log.info("开始查询积分result表根据prtpayNo" + prePayNo);
        List<CurrencyResultDO> list = this.list(map);
        CurrencyResultDO customerBalanceResult = list != null && list.size() > 0 ? list.get(0) : null;
        if (null != customerBalanceResult) {
            customerBalanceResult.setState(CurrencyResultEnumConstants.ENUM_STATE_FAIL);
            customerBalanceResult.setBusinessType(CurrencyResultEnumConstants.ENUM_BUSINESSTYPE_CURRENCYADD);
            customerBalanceResult.setOpeTime(DateFormatHelper.getNowTimeStr());
            customerBalanceResult.setOperType(CurrencyResultEnumConstants.ENUM_OPERTYPE_ADD);
            this.update(customerBalanceResult);
            log.info("积分返还更改积分result表成功根据prtpayNo" + prePayNo);
        }
    }


}
